﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    ///   Represents the system log record entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  | - Text
    ///  + Extended
    ///   | - Description
    ///   + Base
    ///    | - Source
    ///    | - Flags
    ///    | - CreatedOn
    ///    | - UpdatedOn
    ///    + Keys
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class LogRecordEntity : AuditableEntity
    {
        #region Properties

        /// <summary>
        ///   Gets or sets the related source.
        /// </summary>
        public virtual string Source
        {
            get;
            set;
        }

        /// <summary>
        ///   Gets or sets the description.
        /// </summary>
        public virtual string Description
        {
            get;
            set;
        }

        /// <summary>
        ///   Gets or sets the text.
        /// </summary>
        public virtual string Text
        {
            get;
            set;
        }

        /// <summary>
        ///   Gets or sets the flags.
        /// </summary>
        public virtual LogRecordFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["Source"] = Source;
                obj["Flags"] = Flags.ToString();
            }
            if(level >= DisclosureLevel.Extended)
                obj["Description"] = Description;
            if(level >= DisclosureLevel.Entire)
                obj["Text"] = Text;
        }

        #endregion
    }

    /// <summary>
    ///   The entity NHibernate mapping.
    /// </summary>
    public class LogRecordMapping : EntityMapping<LogRecordEntity>
    {
        /// <summary>
        ///   Initializes a new class instance.
        /// </summary>
        public LogRecordMapping()
        {
            Table("fwk_LogRecords");
            Id(x => x.EntityID, m =>
            {
                m.Column("RecordID");
                m.Generator(Generators.Identity);
            });
            Property(x => x.Source, m =>
            {
                m.NotNullable(true);
                m.Length(100);
                m.Column(c =>
                {
                    c.SqlType("nvarchar(100)");
                });
            });
            Property(x => x.Description, m =>
            {
                m.NotNullable(true);
                m.Length(100);
                m.Column(c =>
                {
                    c.SqlType("nvarchar(100)");
                });
            });
            Property(x => x.Text, m =>
            {
                m.NotNullable(true);
                m.Length(Int32.MaxValue);
                m.Column(c =>
                {
                    c.SqlType("nvarchar(max)");
                });
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
        }
    }
}
